$(function () {
    var cyfyys = {
        grid: $("#cyfyys_grid"),
        gridOptions: {
            url: $.webPath() + "/cwgl/kmys/cyfyys/cyfyysList",
            toolbar: '#toolbar_cyfyys',
            fitColumns: true,
            fit: true,
            rownumbers: true,
            pageNumber: 1,
            pagination: true,
            singleSelect: true,
            columns: [
                [
                    {field: 'cyfyys.codeId', title: '代码ID', width: 25},
                    {field: 'nbgsJc', title: '公司名称', width: 50},
                    {field: 'cyfyys.sl', title: '税率', editor: {type: 'validatebox', required: true}, width: 25},
                    {field: 'cyfyys.fydl.z', title: '费用大类', width: 25},
                    {field: 'cyfyys.xtfl.z', title: '系统分类', width: 25},
                    {field: 'cyfyys.fykmqc', title: '费用全称', editor: {type: 'text'}, width: 100},
                    {field: 'cyfyys.fykmjc', title: '费用简称', editor: {type: 'text'}, width: 50},
                    {field: 'cyfyys.kmdm', title: "科目代码", editor: {type: 'text'}, width: 50},
                    {field: 'cyfyys.fylb', title: "费用类别", editor: {type: 'text'}, width: 50},
                    {field: 'cyfyys.cykmdmys.jqcwkmmc', title: "税率科目",
                        editor: {type: 'combobox', options: {valueField: 'jqcwkmmc', textField: 'jqcwkmmc',
                            url: $.webPath() + '/cwgl/kmys/cykmdmys/slcykmdmysList',
                            onBeforeLoad: function (param) {
                                var rowData = $("#cyfyys_grid").datagrid('getSelected');
                                param.gsId = rowData.nbgsId;
                            }, editable: false, required: true}}, width: 170},
                    {field: 'cz', title: '操作', formatter: function (value, row, index) {
                        if (value && value == 'save')
                            return '<a index="' + index + '" name="save" href="javascript:void(0)" onclick="save_cyfyys(' + index + ')"></a>'
                        return '<a index="' + index + '" name="edit" href="javascript:void(0)" onclick="edit_cyfyys(' + index + ')"></a>'
                    }, width: 15, align: 'center'}
                ]
            ],
            onLoadSuccess: function () {
                $("a[name='edit']").linkbutton({
                    plain: true,
                    iconCls: 'icon-edit'
                });
                $("a[name='save']").linkbutton({
                    plain: true,
                    iconCls: 'icon-save'
                });
            },
            onBeforeEdit: function (index, rowData) {
                cyfyys.grid.edatagrid("updateRow", {
                    index: index,
                    row: $.extend({}, rowData, {cz: 'save'})

                });
                $("a[name='save']").linkbutton({
                    plain: true,
                    iconCls: 'icon-save'
                });
            },
            onAfterEdit: function (index, row) {
                var url = $.webPath() + '/cwgl/kmys/cyfyys/save_cyfyys';
                $.post(url, row, function (data) {
                    if (data.statusCode == 200) {
                        cyfyys.grid.datagrid('updateRow', {
                            index: index,
                            row: $.extend({}, row, {cz: 'edit'})
                        });
                        $("a[name='edit']").linkbutton({
                            plain: true,
                            iconCls: 'icon-edit'
                        });
                    } else {
                        $.messager.alert('info', data.message);
                        cyfyys.grid.edatagrid("beginEdit", index);
                    }
                }, 'json');
            }
        },
        search: function () {
            cyfyys.grid.datagrid("load", {
                kmdm: $("#cyfyys_kmdm").val() || '',
                gsId: $("#cyfyys_nbgs").combobox("getValue") || ''
            });
        }
    }
    cyfyys.grid.edatagrid(cyfyys.gridOptions);
    $("#cyfyys_nbgs").gsjgtree(1);
    $("#btn_cyfyys_search").click(function () {
        cyfyys.search();
    });
    $("#btn_cyfyys_insert").click(function () {
        fy_add('新增费用映射', 'add_fy');
    });

});
function save_cyfyys(index) {
    $("#cyfyys_grid").edatagrid("endEdit", index);
    $("#cyfyys_grid").edatagrid("unselectRow", index);
}
function edit_cyfyys(index) {
    $("#cyfyys_grid").edatagrid("beginEdit", index);
    $("#cyfyys_grid").edatagrid("selectRow", index);
}
//新增费用
function fy_add(title, action) {
    var href = $.webPath() + "/cwgl/kmys/cyfyys_content";
    var actionUrl = $.webPath() + "/cwgl/kmys/cyfyys/add_fy";
    fy_showDialog(title, href, actionUrl);
}

//弹出新增或修改的窗口
function fy_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 750,
        height: 330,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#form_cyfyys').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#cyfyys_grid').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}
